package Control;

import Model.student;
import Service.Studentservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/student")
public class studentControl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            req.setCharacterEncoding("utf-8");
            Studentservice studentservice = new Studentservice();
            ArrayList<student> student1 = studentservice.student();
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.write("<html>");
            writer.write("<head>");
            writer.write("<title>学生数据展示</title>");
            writer.write("<meta charset=utf-8>");
            writer.write("</head>");
            writer.write("<body style='background:pink'>");
            writer.write("<div style='width:1000px;margin:0 auto; text-align:center'>");
            writer.write("<h1>学生数据浏览</h1>");
            writer.write("<table cellpadding='5'  cellspacing='3' border='1' width='900' height='550' align='center'>");
            writer.write("<tr><th>学号</th><th>姓名</th><th>年龄</th><th>身高</th><th>学校</th><th>操作1</th><th>操作2</th></tr>");
            for (int i = 0; i < student1.size(); i++) {
                writer.write("<tr>" +
                        "<td>" + student1.get(i).getId() +
                        "</td><td>" + student1.get(i).getName() +
                        "</td><td>" + student1.get(i).getAge() +
                        "</td><td>" + student1.get(i).getHeight() +
                        "</td><td>" + student1.get(i).getSchool() +
                        "</td><td><a onclick='if(confirm(\"确定删除吗\")) return true;return false;' href=del?id=" + student1.get(i).getId() + ">删除</a></td>" +
                        "<td><a href=one?id=" + student1.get(i).getId() + ">更新</a></td>" +
                        "</tr>");
            }
            writer.write("</table>");
            writer.write("<a href='add.jsp'>添加</a>");
            writer.write("</div>");
            writer.write("</body>");
            writer.write("</html>");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
